package com.waimai.service.imlp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waimai.Dao.CategoryMapper;
import com.waimai.common.CustomException;
import com.waimai.domain.Category;
import com.waimai.domain.Dish;
import com.waimai.domain.Setmeal;
import com.waimai.service.CategoryService;
import com.waimai.service.DishService;
import com.waimai.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImlp extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;


    @Autowired
    SetmealService setmealService;


    @Override
    public void remove(Long ids) {
        //查询是否关联菜品
        LambdaQueryWrapper<Dish> dishServiceLambdaQueryWrapper= new LambdaQueryWrapper<>();
        dishServiceLambdaQueryWrapper.eq(Dish::getCategoryId,ids);
        int count1 = dishService.count(dishServiceLambdaQueryWrapper);
        //如果关联则抛异常
        if (count1>0){
            //异常
            throw new CustomException("当前分类下关联了菜品不能删除");
        }
     //////////////////////////////////////////是否关联套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper= new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,ids);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        //如果关联抛出异常
        if (count2>0){
            //异常
            throw new CustomException("当前分类下关联了套餐不能删除");
        }
        //没有关联删除
        super.removeById(ids);



    }
}
